﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using Torrenter.Common.Objects;
using Torrenter.Manager.Services;

namespace Torrenter.Client.Main.ViewModels
{
    public class SearchViewModel : Screen
    {
        #region public properties

        public string ShowName { get { return _showName; } set
        {
            _showName = value;
            NotifyOfPropertyChange(() => ShowName);
                NotifyOfPropertyChange(()=> CanSearch);
        } }
        private string _showName;

        protected int _SeasonNumber;
        public int SeasonNumber
        {
            get { return _SeasonNumber; }
            set
            {
                _SeasonNumber = value;
                NotifyOfPropertyChange(() => SeasonNumber);
            }
        }

        protected int _FromEpisode;
        public int FromEpisode
        {
            get { return _FromEpisode; }
            set
            {
                _FromEpisode = value;
                NotifyOfPropertyChange(() => FromEpisode);
            }
        }

        protected int _ToEpisode;
        public int ToEpisode
        {
            get { return _ToEpisode; }
            set
            {
                _ToEpisode = value;
                NotifyOfPropertyChange(() => ToEpisode);
            }
        }

        protected BindableCollection<Torrent> _Results;

        public BindableCollection<Torrent> Results
        {
            get { return _Results; }
            set
            {
                _Results = value;
                NotifyOfPropertyChange(() => Results);
            }
        }


        #endregion // public properties

        #region actions

        public void Search()
        {
            var s = ShowSeason.GenerateSeason(ShowName, SeasonNumber, FromEpisode, ToEpisode);
            Results = new BindableCollection<Torrent>(s.Episondes);
            var addedTorrents = TorrentManager.Instance.AddTorrent(s);
        }

        public bool CanSearch
        {
            get { return !string.IsNullOrWhiteSpace(ShowName); }
        }

        #endregion // actions

        public SearchViewModel()
        {
            //set default values
            SeasonNumber = 1;
            FromEpisode = 1;
            ToEpisode = 24;
            DisplayName = "Search";
        }

    }
}
